$(function () {
    $(window).resize(function(){
         $("#jqGrid").setGridWidth($(window).width());
         $("#jqGrid").setGridHeight($(window).height());
     });
    $("#jqGrid").jqGrid({
        url: baseURL+'/app/downloadAddress/getAll',
        datatype: "json",
        colModel: [
			{ label: 'ID', name: 'id', width: 30, key: true,hidden:true },
			{ label: '手机系统', name: 'os', width: 10 },
			{ label: '应用商店', name: 'webSiteName', width: 20 },
			{ label: '下载地址', name: 'url', width: 60 },
        ],
        ShrinkToFit:true,
		viewrecords: true,
        height: 605,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        gridview:true, //加速显示
        sortable:true,
        sortname: 'id',
        sortorder: "asc", //排序方式：倒序，本例中设置默认按id倒序排序
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
    });
});
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			key: null
		},
		showList: true,
		title: null,
		downloadAddress: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.downloadAddress = {sts:0,os:'android'};
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}

			$.get(baseURL + "app/downloadAddress/info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.downloadAddress = r.downloadAddress;
            });
		},
		del: function (event) {
			var idsArray= getSelectedRows();
			var ids=idsArray.toString();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "get",
				    url: baseURL + "app/downloadAddress/delete",
                    contentType: "application/json",
				    data:{ids:ids},
				    success: function(r){
						if(r.code == 0){
							alert('操作成功');
							vm.reload();
						}else{
							alert('操作失败');
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
		if($("#download").valid()){
            var url = vm.downloadAddress.id == null ? "app/downloadAddress/save" : "app/downloadAddress/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.downloadAddress),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功');
						vm.reload();
					}else{
						alert('操作失败');
					}
				}
			});
        }
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'key': vm.q.key},
                page:page
            }).trigger("reloadGrid");
		}
	}
});